﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Common.Library;

namespace OnlineVideos
{
    public partial class Browser : PhoneApplicationPage
    {
        string backentryid = string.Empty;
          bool web = false;
        // Constructor
        public Browser()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            FlurryWP8SDK.Api.EndTimedEvent("Browser Page");
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            FlurryWP8SDK.Api.LogPageView();
            if (web == true)
            {
                web = false;
                NavigationService.GoBack();
            }
            
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (ResourceHelper.AppName != Apps.Indian_Cinema_Pro.ToString() && ResourceHelper.AppName != Apps.Kids_TV_Pro.ToString() && ResourceHelper.AppName != Apps.Story_Time_Pro.ToString())
                AppSettings.startplaying = false;
            else
                AppSettings.startplayingforpro = false;
        
            while (NavigationService.BackStack.Count() > 1)
            {
                if (NavigationService.BackStack.FirstOrDefault().Source.OriginalString.Contains("Youtube") || NavigationService.BackStack.FirstOrDefault().Source.OriginalString.Contains("Advertisement"))
                {
                    NavigationService.RemoveBackEntry();

                }
                else
                    break;
            }
            base.OnBackKeyPress(e);
        }
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogEvent("Browser Page", true);
                if (NavigationContext.QueryString.TryGetValue("url", out backentryid))
                {
                    web = true;
                webbrowser.Navigate(new Uri("http://www.youtube.com/embed/"+backentryid+"?autoplay=1&origin=http://example.com", UriKind.Absolute));
                }
               
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In WebBrowserPage file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                ex.Data.Add("Date", DateTime.Now);
                Exceptions.SaveOrSendExceptions("Exception in MainPage_Loaded Method In Browser.cs file.", ex);
            }
          
        }
       
    }
}